//
//  WHTextInfoCell.m
//  SquirrelCourier
//
//  Created by 邓伟浩 on 2019/1/3.
//  Copyright © 2019 邓伟浩. All rights reserved.
//

#import "WHTextInfoCell.h"

@implementation WHTextInfoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI {
    
    [self.contentView addSubview:({
        _inputTextField = [[UITextField alloc] init];
        _inputTextField.font = SYSTEMFONT(kRealwidth(16));
        _inputTextField.textColor = CFontTitleBlackColor;
        _inputTextField;
    })];
    
    [_inputTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left).offset(kRealwidth(15));
        make.right.mas_equalTo(self.contentView.mas_right).offset(-kRealwidth(15));
        make.height.mas_equalTo(self.contentView.mas_height);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [_inputTextField becomeFirstResponder];
}

- (void)prepareForReuse {
    [super prepareForReuse];
    _inputTextField.text = @"";
}

- (void)setInputTextPlaceHolder:(NSString *)placeHolderStr {
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:placeHolderStr attributes:@{NSForegroundColorAttributeName:CFontContentLightGrayColor, NSFontAttributeName:self.inputTextField.font}];
    self.inputTextField.attributedPlaceholder = attrString;
}

- (void)setValidType:(ValideType)validType {
    _validType = validType;
    
    kWeakSelf(self);
    if (validType == ValideTypePassword) {
        self.inputTextField.keyboardType = UIKeyboardTypeASCIICapable;
        self.inputTextField.yb_inputCP = YBInputControlProfile.creat.set_maxLength(12).set_regularStr(@"^[a-zA-Z0-9]{0,12}$").set_textChanged(^(id obj) {
            if (weakself.textChangeBlock) {
                weakself.textChangeBlock([obj valueForKey:@"text"]);
            }
        });
    } else if (self.validType == ValideTypePhoneNumber) {
        self.inputTextField.keyboardType = UIKeyboardTypeNumberPad;
        self.inputTextField.yb_inputCP = YBInputControlProfile.creat.set_maxLength(11).set_regularStr(@"^[0-9]{0,11}+$").set_textChanged(^(id obj) {
            if (weakself.textChangeBlock) {
                weakself.textChangeBlock([obj valueForKey:@"text"]);
            }
        });
    }
}

@end
